<template>
  <div class="alarm-setUp">
    <el-row>
      <el-col :span="24">
        <dynForm
          :info-form="infoForm"
          :search-tags="searchTags"
          :select-list="selectList"
        >
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t("search") }}
          </el-button>
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-plus"
        @click="newAdd"
      >{{ $t("add") }}</el-button
      >
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="doModify"
      >{{ $t("modify") }}</el-button
      >
      <el-button
        v-btnPermission:delete
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="doDelete"
      >{{ $t("delete") }}</el-button
      >
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-download"
        @click="downloadFile"
      >{{ $t("downloadTemp") }}</el-button>
      <el-upload
        :action="baseApi + '/ams/upload/submit?name=AlarmSetUpList'"
        :headers="headers"
        :show-file-list="false"
        :on-success="uploadSuccess"
        :on-error="importFail"
        class="upload-btn"
        style="display: inline-block"
        accept=".xlsx"
      >
        <el-button
          v-btnPermission:add
          type="primary"
          icon="el-icon-upload"
        >{{ $t("import") }}</el-button
        >
      </el-upload>
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-download"
        @click="doExport"
      >{{ $t("export") }}
      </el-button>
    </div>
    <!-- 表单部分 -->
    <el-card shadow="never">
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <!-- 弹窗组件 -->
    <setupForm ref="setupForm" @initList="initList"/>
  </div>
</template>

<script>
import mySetting from '@/mixins/mySetting'
import setupForm from './components/alarmSetupForm'
import alarmSetUpApi from '@/api/alarm/setup'
import { betterTips } from '@/utils/notification'
import { getEqpModelList } from '@/api/equipment/eqpbase'
import { getAllActions } from '@/api/alarm/action'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import { download } from '@/api/system/user'
export default {
  name: 'AlarmSetUp',
  components: { setupForm },
  mixins: [mySetting],
  props: {},
  data() {
    return {
      infoForm: {},
      searchTags: [
        // { label: this.$t('eqpModel'), prop: 'eqpModel', tag: 'DynSelect', labelwidth: '90px' },
        { label: this.$t('alarmId'), prop: 'alarmId', tag: 'DynInput', labelwidth: '90px' },
        // { label: this.$t('alarmAction'), prop: 'alarmAction', tag: 'DynSelect', labelwidth: '90px' },
        { label: this.$t('eqpId'), prop: 'eqpId', tag: 'DynInput', labelwidth: '90px' }
      ],
      selectList: {},
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        { prop: 'alarmId', label: this.$t('AlarmCode') },
        // { prop: 'eqpModel', label: this.$t('eqpModel') },
        { prop: 'eqpId', label: this.$t('eqpId') },
        { prop: 'eqpModel', label: this.$t('nodeLevel') },
        { prop: 'alarmLevel', label: this.$t('AlarmLevel') },
        { prop: 'actions', label: this.$t('ExecAction') },
        { prop: 'effectiveTime', label: this.$t('effectiveTime') },
        { prop: 'repeatCount', label: this.$t('repeatCount') },
        { prop: 'description', label: this.$t('description') },
        { prop: 'createTimeStr', label: this.$t('createTime') }
      ],
      tableLoading: true,
      headers: {
        Token: getToken()
      }
    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  created() {
    this.getEqpTypesList()
    this.getActionsList()
    this.initList()
  },
  methods: {
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    async initList() {
      this.tableLoading = true
      const res = await alarmSetUpApi.getList(this.$clearNull({ ...this.infoForm, ...this.listQuery }))
      this.tableData = res.data
      // this.tableData = [{ id: 1, actions: 'HoldLot,HoldLotHoldLotHoldLotHo,ldLotHoldLotHol,dLotHoldLotHoldLotHoldLotHoldLotHoldLotHoldLotHoldLotHoldLotHoldLotHoldLotHoldLotHoldLot' }]

      this.total = res.total
      this.tableLoading = false
    },
    newAdd() {
      this.$refs.setupForm.handleOpen()
    },
    doModify() {
      this.$refs.setupForm.handleOpen(this.multipleSelection[0])
    },
    // 删除
    doDelete() {
      this.$confirm(
        `${this.$t('confirmDelete')}${this.multipleSelection.length}${this.$t(
          'recordCount'
        )}?`,
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map((ele) => ele.id)
        alarmSetUpApi.doDelete({ ids }).then((res) => {
          betterTips(res, this.setOnePage, this.initList)
        })
      })
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    getList(value) {
      this.listQuery = value
      this.initList()
    },
    async doExport() {
      const res = await alarmSetUpApi.getList(this.$clearNull({
        ...this.infoForm,
        ...this.listQuery,
        toexcel: 1
      }))
      window.open(this.baseApi + '/' + res.data)
    },
    downloadFile() {
      download({ name: 'AlarmSetUpList' }).then((res) => {
        window.open(this.baseApi + '/' + res.data)
      })
    },
    uploadSuccess(response, file, fileList) {
      // 第{ele.index}行 {this.$t(ele.msgCode)}
      // 第{ele.split(',')[0]}行 {this.$t(ele.split(',')[1])}
      // this.netTips(response.code)
      if (response.code !== 1) {
        const h = this.$createElement
        const listVNode = response.data.map((item) => {
          return (
            <p style={ { width: '240px' }}>
              <span style={ { width: '100%', wordWrap: 'break-word' }}>{item.index ? `第{${item.index}}行` : ''}{this.$t(item.msgCode)}</span>
            </p>
          )
        })
        const styles = {
          color: 'teal',
          overflowX: 'hidden',
          overflowY: 'auto',
          width: '280px',
          maxHeight: 'calc(100vh - 120px)'
        }
        this.$notify({
          message: h('div', { style: styles }, listVNode),
          type: 'error',
          duration: 0
        })
        fileList = []
      } else {
        this.$betterTips(response, this.initList)
      }
    },
    importFail() {
      this.$notify({
        message: this.$t('uploadFail'),
        type: 'error',
        duration: 3500
      })
    },
    async getEqpTypesList() {
      const res = await getEqpModelList()
      this.$set(this.selectList, 'eqpModel', res.data)
    },
    async getActionsList() {
      const res = await getAllActions()
      this.$set(this.selectList, 'alarmAction', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/defaultStyle.scss";
.upload-btn{
  margin: 0 10px;
}
</style>
